//
// Created by 86131 on 2023/9/11.
//
#include <iostream>
#include <stdio.h>
using namespace std;
typedef int ElementType;
typedef struct Node *PtrToNode;
struct Node {
    ElementType Data;//存储结点数据
    PtrToNode   Next = nullptr;//指向下一个结点的指针
};
typedef PtrToNode List;//定义单链表类型;

List Read(); /* 细节在此不表 */
void Print( List L ); /* 细节在此不表；空链表将输出NULL */

List Merge( List L1, List L2 );
List jj(List L1 ,List L2);

int main()
{
    List L1, L2, L;
    L1 = Read();
    L2 = Read();
    L = jj(L1, L2);
    Print(L);
    return 0;
}
List Read() {
    int n;//
    cin >> n;//
    List first = (List) malloc(sizeof(List));
    List node_temp;
    node_temp = first;
    while (n != -1) {
        Node *node = new Node;
        node->Data = n;
        node_temp->Next = node;
        node_temp = node_temp->Next;
        cin>>n;
    }
    return first;
}
void Print(List L){
    if(L->Next== nullptr){cout<<"NULL";
        return;}
    L = L->Next;
    while(L!=nullptr){
        cout<<L->Data;
        L= L->Next;
        if(L!=nullptr){cout<<" ";}
    }

}
List jj( List L1, List L2 ){
    if(!L1 &&!L2){return NULL;}
    List p1 = L1->Next;
    List p2 = L2->Next;
    // L1 = L1->Next;
    // L2 = L2->Next;
    List L = (List)malloc(sizeof (List));
    L->Next = NULL;
    List node_temp;
    node_temp = L;
    while(p1 && p2){
        if(p1->Data<p2->Data){p1 =p1->Next;
            continue;}
        if(p1->Data==p2->Data){node_temp->Next = p1;p1 =p1->Next;p2=p2->Next;node_temp = node_temp->Next;
            continue; }
        if(p1->Data>p2->Data){p2 =p2->Next;
            continue;}
    }
    node_temp->Next = NULL;
    L1->Next = NULL;
    L2->Next = NULL;
    return L;
}